package com.nice.price;

import com.alibaba.fastjson.JSONObject;
import com.nice.BeanUtil;
import com.nice.Config;
import com.nice.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AutdPrice {

    private final static Logger logger = LoggerFactory.getLogger(AutdPrice.class);
    //@Autowired 注入不进来。
    private Config config;
    public AutdPrice() {
        config= (Config) BeanUtil.getBean("config");
    }

    public List getAutdPrice() {
        List list = new ArrayList();
        String result = "";
        JSONObject jsonObject;
        try {
            logger.info("开始查询贵金属价格");
            jsonObject = HttpClientUtil.doGet(
                    "http://api.k780.com/?app=finance.shgold&goldid=1051&version=3&appkey="+config.getAppkey()+"&sign="+config.getSign()+"&format=json");
            result = jsonObject.getString("result");

            JSONObject resultJsonObject =jsonObject.getJSONObject("result");
            String sell_price = resultJsonObject.getString("sell_price");
            String buy_price = resultJsonObject.getString("buy_price");
            String varietynm = resultJsonObject.getString("varietynm");
            String uptime = resultJsonObject.getString("uptime");
            String open_price = resultJsonObject.getString("open_price");
            String change_price = resultJsonObject.getString("change_price");
            String change_margin = resultJsonObject.getString("change_margin");


            Map map = new HashMap();
            map.put("sell_price", sell_price);
            map.put("buy_price", buy_price);
            map.put("name", "AUTD");
            map.put("uptime", uptime);
            map.put("open_price", open_price);
            map.put("change_price", change_price);
            map.put("change_margin", change_margin);

            list = new ArrayList();
            list.add(map);

            logger.info("查询贵金属价格{}", result);
        } catch (Exception e) {
            logger.error("查询贵金属价格异常{}", e);
        }
        return list;
    }
}
